/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figures 3

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/Fig3`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/AKM_Firm_FE_for_export_stata.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/

	rename obs total_obs

	** Figure 3.A

	*Winsorize (trim) Firm Fixed Effects
	winsor2 firm_fe4, replace cuts(1 99) trim
	winsor2 uf_fe4, replace cuts(1 99) trim

	*Binscatter User firm FE vs Regular Firm FE
	binscatter uf_fe4 firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & connected_set == 1,  savedata(figure_3_A) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) lcolor(black) mcolor(black)

	*Compute coefficients for Displayed Figure 3.A
	reg uf_fe4 firm_fe4 [aw=total_obs] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1
	local beta_firm_fe4 = _b[firm_fe4]
	local se_firm_fe4 = _se[firm_fe4]
	local constant = _b[_cons]

	preserve

	clear
	qui do figure_3_A

	*Normalization
	sum  firm_fe4 if _n == 1
	local mean_reg = r(mean)
	replace firm_fe4 = firm_fe4 - `mean_reg'
	sum  uf_fe4 if _n == 1
	local mean_uf = r(mean)
	replace uf_fe4 = uf_fe4 - `mean_uf'

	*Plot Figure 3.A
	twoway function y= x, range(0 1) color(black) lpattern(dash) || function y = 0, range(0 1) color(black) lpattern(dash) || scatter uf_fe4 firm_fe4, mcolor(black) msize(medlarge) || function y= `beta_firm_fe4'*x , range(0 1) color(black)   /*
	*/ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.9 .78 "Benchmark for Insiders:" "{&gamma} = 1") text(.1 .78 "Competitive Benchmark:" "{&gamma} = 0") text(.52 .78 "{&gamma} = 0.49 (SE 0.0075)")
	graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope.pdf", replace

	restore

	********* WEIGHTED BY TEMP OBS ***********
	binscatter uf_fe4 firm_fe4 [aw=obs_temp] if firm_fe4 != 0 & firm_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & connected_set == 1,  savedata(3a_tempweights) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9)

	reg uf_fe4 firm_fe4 [aw=obs_temp] if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1
	local beta_firm_fe4 = _b[firm_fe4]
	local se_firm_fe4 = _se[firm_fe4]
	local constant = _b[_cons]

	preserve

	clear
	qui do 3a_tempweights

	sum  firm_fe4 if _n == 1
	local mean_reg = r(mean)
	replace firm_fe4 = firm_fe4 - `mean_reg'
	sum  uf_fe4 if _n == 1
	local mean_uf = r(mean)
	replace uf_fe4 = uf_fe4 - `mean_uf'

	*Figure 3.A with Temp Weights
	twoway function y= x, range(0 1) color(black) lpattern(dash) || function y = 0, range(0 1) color(black) lpattern(dash) || scatter uf_fe4 firm_fe4, mcolor(black) msize(medlarge) || function y= `beta_firm_fe4'*x , range(0 1) color(black)   /*
	*/ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.9 .78 "Benchmark for Insiders:" "{&gamma} = 1") text(.1 .78 "Competitive Benchmark:" "{&gamma} = 0") text(.6 .78 "{&gamma} = 0.61 (SE 0.0055)")
	graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope_TEMPweights.pdf", replace

	restore

	** Figure 3.B
	*Generate variables for changes in Fixed effects
	gen firm_fe4_change = firm_fe4_gd1 - firm_fe4_gd0
	gen uf_fe4_change = uf_fe4_gd1 - uf_fe4_gd0

	*Winsorize (trim) Firm Fixed Effect
	winsor2 firm_fe4_change, replace cuts(1 99) trim
	winsor2 uf_fe4_change, replace cuts(1 99) trim

	*Binscatter User firm FE vs Regular Firm FE (in changes)
	binscatter uf_fe4_change firm_fe4_change [aw=total_obs] if firm_fe4_gd0 != . & firm_fe4_gd0 != 0 & firm_fe4_gd1 != . & firm_fe4_gd1 != 0 & uf_fe4_gd0 != 0 & uf_fe4_gd0 != . & uf_fe4_gd1 != 0 & uf_fe4_gd1 != . & connected_set == 1, savedata(figure_3_b) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) lcolor(black) mcolor(black)
	graph export "${Results}/Binscatter_akm4_firmFE_changes.pdf", replace

	*Compute coefficients for Displayed Figure 3.B
	reg uf_fe4_change firm_fe4_change [aw=total_obs] if firm_fe4_gd0 != . & firm_fe4_gd0 != 0 & firm_fe4_gd1 != . & firm_fe4_gd1 != 0 & uf_fe4_gd0 != 0 & uf_fe4_gd0 != . & uf_fe4_gd1 != 0 & uf_fe4_gd1 != . & connected_set == 1
	local beta_firm_fe4_change = _b[firm_fe4_change]
	local se_firm_fe4_change = _se[firm_fe4_change]
	local constant_2 = _b[_cons]

	preserve

	clear
	qui do figure_3_b

	*Normalization
	sum firm_fe4_change if _n == 1
	local mean_reg = r(mean)
	replace firm_fe4_change = firm_fe4_change - `mean_reg'
	local norm_var = `constant_2' + `beta_firm_fe4_change' * `mean_reg'
	replace uf_fe4_change = uf_fe4_change - `norm_var'

	*Plot Figure 3.B
	twoway function y= x, range(0 .5) color(black) lpattern(dash) || function y = 0, range(0 .5) color(black) lpattern(dash) || scatter uf_fe4_change firm_fe4_change, mcolor(black) msize(medlarge) || function y= `beta_firm_fe4_change'*x, color(black) range(0 .5) /*
	*/ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 (.1) .5)) ysc(r(0 (.1) .5)) ylabel(0(.1).5,nogrid) xlabel(0(.1).5) xsize(16) ysize(9) text(.05 .42 "Competitive Benchmark:" "{&gamma} = 0") text(.5 .42 "Benchmark for Insiders:" "{&gamma} = 1") text(.22 .42 "{&gamma} = 0.37 (SE 0.0308)")
	graph export "${Results}/ScatterPlot_akm4_change_firmFE.pdf", replace

	restore

log close
